package com.fastgwt.client.mvc.view;

import java.util.ArrayList;
import java.util.List;

import com.fastgwt.client.Fastgwt;
import com.fastgwt.client.model.EmployeeProperties;
import com.fastgwt.shared.dto.Employee;
import com.fastgwt.shared.services.EmployeeService;
import com.fastgwt.shared.services.EmployeeServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.state.client.GridStateHandler;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.AbstractHtmlLayoutContainer.HtmlData;
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.RowDoubleClickEvent;
import com.sencha.gxt.widget.core.client.event.RowDoubleClickEvent.RowDoubleClickHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FormPanel.LabelAlign;
import com.sencha.gxt.widget.core.client.form.FormPanelHelper;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;

public class EmployeeQueryView extends EmployeeView {
	private static final int COLUMN_FORM_WIDTH = 1000;
	private static final EmployeeProperties props2 = GWT
			.create(EmployeeProperties.class);
	TextButton listBtn;
	TextButton clearBtn;
	ListStore<Employee> store;
	Grid<Employee> grid;

	@Override
	protected void initialize() {
		// TODO Auto-generated method stub
		// super.layoutManagement();
		store = new ListStore<Employee>(props2.key());

		listBtn = new TextButton("List");
		clearBtn = new TextButton("Clear");
//		VerticalLayoutContainer p = new VerticalLayoutContainer();
//		this.add(p);
		this.setWidth(1000);

		HtmlLayoutContainer con = new HtmlLayoutContainer(getTableMarkup());
		this.setWidget(con);
		int cw = (COLUMN_FORM_WIDTH / 2) - 12;
		// firstName.setAllowBlank(false);
		employeeEditor.getEmployeeName().setEmptyText("Enter name...");
		employeeEditor.getEmployeeName().setWidth(cw);

		// lastName.setAllowBlank(false);
		employeeEditor.getEmployeeSurname().setEmptyText("Enter last name...");
		employeeEditor.getEmployeeSurname().setWidth(cw);

		// job.setAllowBlank(false);
		employeeEditor.getJob().setEmptyText("Enter job description...");
		employeeEditor.getJob().setWidth(cw);

		con.add(new FieldLabel(employeeEditor.getEmployeeName(), "First Name"),
				new HtmlData(".fn"));

		con.add(new FieldLabel(employeeEditor.getEmployeeSurname(), "Last Name"),
				new HtmlData(".ln"));

		con.add(new FieldLabel(employeeEditor.getJob(), "Job Description"),
				new HtmlData(".job"));

		HorizontalPanel hp = new HorizontalPanel();
		hp.add(listBtn);
		hp.add(clearBtn);
		hp.setSpacing(10);
		con.add(hp, new HtmlData(".btn"));

		// this.addButton(listBtn);
//
//		con.add(clearBtn, new HtmlData(".clearBtn"));

		// this.addButton(clearBtn);

		List<FieldLabel> labels = FormPanelHelper.getFieldLabels(this);
		for (FieldLabel lbl : labels) {
			lbl.setLabelAlign(LabelAlign.TOP);
		}

		ColumnConfig<Employee, String> nameCol = new ColumnConfig<Employee, String>(
				props2.employeeName(), 50, "Name");

		ColumnConfig<Employee, String> surnameCol = new ColumnConfig<Employee, String>(
				props2.employeeSurname(), 50, "Surname");

		ColumnConfig<Employee, String> jobCol = new ColumnConfig<Employee, String>(
				props2.job(), 50, "Job");

		ColumnConfig<Employee, String> emailCol = new ColumnConfig<Employee, String>(
				props2.email(), 150, "Email");

		List<ColumnConfig<Employee, ?>> l = new ArrayList<ColumnConfig<Employee, ?>>();
		l.add(nameCol);
		l.add(surnameCol);
		l.add(jobCol);
		l.add(emailCol);
		ColumnModel<Employee> cm = new ColumnModel<Employee>(l);
		store = new ListStore<Employee>(props2.key());
		grid = new Grid<Employee>(store, cm);
		grid.getView().setAutoExpandColumn(nameCol);
		grid.getView().setStripeRows(true);
		grid.getView().setColumnLines(true);
		grid.setBorders(false);
		grid.setWidth(COLUMN_FORM_WIDTH);
		grid.setColumnReordering(true);
		grid.setStateful(true);
		grid.setStateId("gridExample");

		con.add(grid, new HtmlData(".mygrid"));
		// GridStateHandler<Employee> state = new
		// GridStateHandler<Employee>(grid);
		// state.loadState();

	}

	private native String getTableMarkup() /*-{
		return [
				'<table width=100% cellpadding=0 cellspacing=0>',
				'<tr><td class=fn width=120px></td><td class=ln width=120px></td></tr>',
				'<tr><td class=job width=120px></td><td ></td></tr>',
				'<tr><td ></td><td class=btn></td></tr>',
				'<tr><td colspan=2 class=mygrid ></td></tr>'

		].join("");
	}-*/;

	public TextButton getListBtn() {
		return listBtn;
	}

	public TextButton getClearBtn() {
		return clearBtn;
	}

	public ListStore<Employee> getStore() {
		return store;
	}

	public void setStore(ListStore<Employee> store) {
		this.store = store;
	}

	public Grid<Employee> getGrid() {
		return grid;
	}

	public void setGrid(Grid<Employee> grid) {
		this.grid = grid;
	}
}
